package com.hjq.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hjq.common.PageResult;
import com.hjq.entity.vo.CollectionCourseVO;
import com.hjq.entity.vo.PageParam;
import com.hjq.mapper.CollectionMapper;
import com.hjq.service.CollectionService;
import com.hjq.utils.CurrentHolder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class CollectionServiceImpl implements CollectionService {
    @Autowired
    private CollectionMapper collectionMapper;

    @Override
    public PageResult<CollectionCourseVO> findByUserIdAndPage(PageParam pageParam) {
        if (pageParam.getPageNum() == null) {
            pageParam.setPageNum(1);
        }
        if (pageParam.getPageSize() == null) {
            pageParam.setPageSize(10);
        }
        Long currentId = CurrentHolder.getCurrentId();
        PageHelper.startPage(pageParam.getPageNum(), pageParam.getPageSize());
        List<CollectionCourseVO> result = collectionMapper.findByUserId(currentId);
        PageInfo<CollectionCourseVO> pageInfo = new PageInfo<>(result);
        return new PageResult<>(pageInfo.getList(), pageInfo.getTotal());
    }

    @Override
    public Integer add(Long courseId) {
        Long userId = CurrentHolder.getCurrentId();
        return collectionMapper.add(userId,courseId);
    }

    @Override
    public Integer delete(Long id) {
        return collectionMapper.delete(id);
    }


}
